﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Courier.Core;
using Umbraco.Courier.ItemProviders;

namespace Contour.Addons.CourierSupport.DataResolvers
{
    public class FormPicker : ItemDataResolverProvider
    {
        private Guid FormPickerGuid = new Guid("60A73E32-C48D-11DE-B452-FC9F55D89593");

        public override List<Type> ResolvableTypes
        {
            get { return new List<Type>() { typeof(ContentPropertyData) }; }
        }

        public override bool ShouldExecute(Item item, Umbraco.Courier.Core.Enums.ItemEvent itemEvent)
        {
            ContentPropertyData cpd = (ContentPropertyData)item;
            return (cpd.Data.Where(x => x.DataTypeEditor == FormPickerGuid).Count() > 0);
        }

        public override void Packaging(Item item)
        {
            ContentPropertyData cpd = (ContentPropertyData)item;
            foreach (var cp in cpd.Data)
            {
                Guid guid;

                if (cp.Value != null && cp.DataTypeEditor == FormPickerGuid && Guid.TryParse(cp.Value.ToString(), out guid))
                {
                    Dependency formDep = new Dependency();
                    formDep.ItemId = new ItemIdentifier(guid.ToString(), ProviderIDCollection.formItemProviderGuid);
                    formDep.Name = "Form from form picker";

                    item.Dependencies.Add(formDep);
                }
            }
        }

        public override void Extracting(Item item)
        {
            ContentPropertyData cpd = (ContentPropertyData)item;
            foreach (var cp in cpd.Data)
            {
                if (cp.Value != null && cp.DataTypeEditor == FormPickerGuid)
                {

                }
            }
        }
    }
}